rm(list =ls())
options(scipen=999)
gc()
packages <-c("tidyverse","estimatr","plm","stargazer",
             "fastDummies","ICCbin","ihs","readstata13","xtable")

new.packages <- packages[!(packages %in% installed.packages()[,"Package"])]
if(length(new.packages)) install.packages(new.packages)

lapply(packages, require, character.only = TRUE)
rm(packages, new.packages)

setwd("PUT YOUR DIRECTORY HERE")

## insheet data
data <- read.csv("./Datasets/panel_bare_bones.csv")

## clean data
data$X <- NULL
data$locality_year <- NULL
data$subbotnik_inkind <- NULL

# generate dummies for year and locality
data$year <- as.character(data$year)
data$locality <- as.character(data$locality)
data <- fastDummies::dummy_cols(data)
data$year <- as.numeric(data$year)



###############################
###############################
### REGRESSIONS FOR STRIKES ###
###############################
###############################

##############
## Cleaning ##
##############

# subset to years with observations:
data_t <- data[which(data$year>1979),]

# generate lags and leads of protests

# function
create_lag <- function(outcome, year_shift) {
  if (year_shift > 0) {return (c(rep(NA, year_shift), outcome)[1 : length(outcome)] );}
  else {return (c(outcome[(-year_shift + 1) : length(outcome)], rep(NA, -year_shift)));}
  }

# assign variables
data_t$strikes_lag_1 <- create_lag(data_t$strikes, 1)
data_t$strikes_lead_1 <- create_lag(data_t$strikes, -1)
data_t$strikes_lag_2 <- create_lag(data_t$strikes, 2)
data_t$strikes_lead_2 <- create_lag(data_t$strikes, -2)

# scale protest variables and leads
data_t$strikes <- scale(data_t$strikes)
data_t$strikes_lag_1 <- scale(data_t$strikes_lag_1)
data_t$strikes_lag_2 <- scale(data_t$strikes_lag_2)


########################
## Simple OLS; no FEs ##
########################

# no lags
m_repression_ols <- lm(strikes ~ commanders, data = data_t)

# 1 lag
m_repression_ols_lag_1 <- lm(strikes ~ commanders + strikes_lag_1, data = data_t)

# 2 lags
m_repression_ols_lag_2 <- lm(strikes ~ commanders + strikes_lag_1 + strikes_lag_2, data = data_t)


############################
## Locality Fixed Effects ##
############################

# Locality FEs no lag
m_repression_id_fe <- plm(strikes ~ commanders, data = data_t, index = c("locality"), model = "within")

# Locality FEs with 1 lags
m_repression_id_fe_lag_1 <- plm(strikes ~ commanders + strikes_lag_1, data = data_t, index = c("locality"), model = "within")

# Locality FEs with 2 lags
m_repression_id_fe_lag_2 <- plm(strikes ~ commanders + strikes_lag_1+ strikes_lag_2, data = data_t, index = c("locality"), model = "within")


##############
## Time FEs ##
##############

# Year FEs no lag
m_repression_year_fe <- plm(strikes ~ commanders, data = data_t, index = c("year"), model = "within")

# Locality FEs with 1 lags
m_repression_year_fe_lag_1 <- plm(strikes ~ commanders + strikes_lag_1, data = data_t, index = c("year"), model = "within")

# Locality FEs with 2 lags
m_repression_year_fe_lag_2 <- plm(strikes ~ commanders + strikes_lag_1+ strikes_lag_2, data = data_t, index = c("year"), model = "within")



#######################
## Unit and Time FEs ##
#######################

# Year and locality FEs no lag
m_repression_year_locality_fe <- plm(strikes ~ commanders, data = data_t, index = c("year", "locality"), model = "within", effect = "twoways")

# Year and locality FEs with 1 lags
m_repression_year_locality_fe_lag_1 <- plm(strikes ~ commanders + strikes_lag_1, data = data_t, index = c("year", "locality"), model = "within")

# Year and locality FEs with 2 lags
m_repression_year_locality_fe_lag_2 <- plm(strikes ~ commanders + strikes_lag_1 + strikes_lag_2, data = data_t, index = c("year", "locality"), model = "within")


########################################
########################################
## REGRESSIONS FOR SUBBOTNIK (Inkind) ##
########################################
########################################

##############
## Cleaning ##
##############

# subset to years with observations:
data_t <- data[which(data$year<1980),]

# omit missings
data_t <- data_t[,c("locality_numeric", "year", "commanders", "subbotnik_inkind_", "year_1975", "year_1976", "year_1977", "year_1978", "year_1979")]
data_t <- na.omit(data_t) 

# turn subbotnik upside down so that it means sabotage
data_t$subbotnik_inkind_ <- data_t$subbotnik_inkind_*-1

# generate lags and leads of sabotage

# function
create_lag <- function(outcome, year_shift) {
  if (year_shift > 0) {return (c(rep(NA, year_shift), outcome)[1 : length(outcome)] );}
  else {return (c(outcome[(-year_shift + 1) : length(outcome)], rep(NA, -year_shift)));}
}

# assign variables
data_t$subbotnik_inkind__lag_1 <- create_lag(data_t$subbotnik_inkind_, 1)
data_t$subbotnik_inkind__lead_1 <- create_lag(data_t$subbotnik_inkind_, -1)
data_t$subbotnik_inkind__lag_2 <- create_lag(data_t$subbotnik_inkind_, 2)
data_t$subbotnik_inkind__lead_2 <- create_lag(data_t$subbotnik_inkind_, -2)

# scale subbotnik variables and lags / leads
data_t$subbotnik_inkind_ <- scale(data_t$subbotnik_inkind_)
data_t$subbotnik_inkind__lag_1 <- scale(data_t$subbotnik_inkind__lag_1)
data_t$subbotnik_inkind__lag_2 <- scale(data_t$subbotnik_inkind__lag_2)


########################
## Simple OLS; no FEs ##
########################

# no lags
m_subbotnik_inkind_ols <- lm(subbotnik_inkind_ ~ commanders, data = data_t)

# 1 lag
m_subbotnik_inkind_ols_lag_1 <- lm(subbotnik_inkind_ ~ commanders + subbotnik_inkind__lag_1, data = data_t)

# 2 lags
m_subbotnik_inkind_ols_lag_2 <- lm(subbotnik_inkind_ ~ commanders + subbotnik_inkind__lag_1 + subbotnik_inkind__lag_2, data = data_t)



############################
## Locality Fixed Effects ##
############################

#*****SHOULDN'T DO THIS BECAUSE SAMPLE IS N = 2****


##############
## Time FEs ##
##############

# Year FEs no lag
m_subbotnik_inkind_year_fe <- plm(subbotnik_inkind_ ~ commanders, data = data_t, index = c("year"), model = "within")

# Year FEs with 1 lag
m_subbotnik_inkind_year_fe_lag_1 <- plm(subbotnik_inkind_ ~ commanders + subbotnik_inkind__lag_1, data = data_t, index = c("year"), model = "within")

# Year FEs with 2 lags
m_subbotnik_inkind_year_fe_lag_2 <- plm(subbotnik_inkind_ ~ commanders + subbotnik_inkind__lag_1 + subbotnik_inkind__lag_2, data = data_t, index = c("year"), model = "within")


############
## Output ##
############

stargazer(m_repression_ols_lag_1,
          m_repression_ols_lag_2,
          m_repression_id_fe_lag_1,
          m_repression_id_fe_lag_2,
          m_repression_year_fe_lag_1,
          m_repression_year_fe_lag_2,
          m_repression_year_locality_fe_lag_1,
          m_repression_year_locality_fe_lag_2,
          m_subbotnik_inkind_ols_lag_1,
          m_subbotnik_inkind_ols_lag_2,
          m_subbotnik_inkind_year_fe_lag_1,
          m_subbotnik_inkind_year_fe_lag_2,
          dep.var.labels = c("Protest (z)", "Resistance (z)"),
          style = "qje",
          covariate.labels = c("Repression (cont.)"),
          star.char = c("*", "**", "***"),
          star.cutoffs = c(0.1, 0.05, 0.01),
          omit = c("Constant", "subbotnik", "strikes"),
          omit.stat = c("rsq", "f", "ser"), 
          omit.table.layout = "n",
          add.lines = list(c("Lags", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2", "1", "2"),
                           c("Year fixed effects", "No", "No", "Yes", "Yes", "No", "No", "Yes", "Yes", "No", "No", "Yes", "Yes"),
                           c("Locality fixed effects", "No", "No", "No", "No", "Yes", "Yes", "Yes", "Yes", "No", "No", "No", "No")),
          out = "PUT YOUR FILEPATH HERE")


